package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.entity.SysUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Resource(name = "sysUserService")
    private SysUserService sysUserService;
    @Resource(name = "passwordEncoder")
    private PasswordEncoder passwordEncoder;

    @GetMapping("/pass")
    public Result<Boolean> updatePassword(String password, String newPassword) throws IframeException {
        SysUser sysUser = sysUserService.getById(SystemContextHolder.getSessionContext().getUserId());
        if(!passwordEncoder.matches(password, sysUser.getPassword())){
            return Result.error("旧密码错误");
        }
        return Result.ok(sysUserService.update(sysUser));
    }

}
